﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using SimpleCalcParser;
using SimpleCalcDataStructures;

namespace WP7_SimpleCalc 
{
    public partial class MainPage : PhoneApplicationPage
    {
        //stack type operation
        private LinkedList<string> previousEntries;
        private LinkedListNode<string> stackPtr;

        private bool insMode;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            previousEntries = new LinkedList<string>();
            insMode = true;
        }

        //Input button functions
        private void numButton_Click(object sender, RoutedEventArgs e)
        {
            inputDisplayBox.Text += ((Button)sender).Content;
        }

        private void negate_Click(object sender, RoutedEventArgs e)
        {
            inputDisplayBox.Text += "-";
        }

        private void binaryOperator_Click(object sender, RoutedEventArgs e)
        {
            //if nothing entered default to previous answer
            if (inputDisplayBox.Text == "")
            {
                inputDisplayBox.Text += valueDisplayBox.Text;
            }
            inputDisplayBox.Text += ((Button)sender).Content;
        }

        private void function_Button_Click(object sender, RoutedEventArgs e)
        {
            inputDisplayBox.Text += ((Button)sender).Content+"(";
        }

        private void exponent_Button_Click(object sender, RoutedEventArgs e)
        {
            inputDisplayBox.Text += "^";
        }

        private void squared_Button_Click(object sender, RoutedEventArgs e)
        {
            inputDisplayBox.Text += "^2";
        }

        private void AnswerButton_Click(object sender, RoutedEventArgs e)
        {
            inputDisplayBox.Text += valueDisplayBox.Text;
        }

        //Clear and Delete Operations
        private void clear_Button_Click(object sender, RoutedEventArgs e)
        {
            inputDisplayBox.Text = "";
            valueDisplayBox.Text = "";
        }

        private void delete_Button_Click(object sender, RoutedEventArgs e)
        {
            if (inputDisplayBox.Text.Length > 0)
            {
                inputDisplayBox.Text = inputDisplayBox.Text.Substring(0, inputDisplayBox.Text.Length - 1);
            }
        }
        
        //Equals button-> Parser
        private void equalsButton_Click(object sender, RoutedEventArgs e)
        {
            if (inputDisplayBox.Text == "")
                return;
            Parser parse = new Parser();
            Operand resultVal = parse.Parse(inputDisplayBox.Text);
            valueDisplayBox.Text = resultVal.Value.ToString();
            //add to Stack
            previousEntries.AddLast(inputDisplayBox.Text);
            stackPtr = previousEntries.Last;
            if (previousEntries.Count == 6)
            {
                previousEntries.RemoveFirst();
            }
            inputDisplayBox.Text = "";
        }

        private void upArrow_Click(object sender, RoutedEventArgs e)
        {
            if (insMode && stackPtr!=null)
            {
                inputDisplayBox.Text = stackPtr.Value;
                if (stackPtr.Previous != null)
                {
                    stackPtr = stackPtr.Previous;
                }
            }
        }

        private void downArrow_Click(object sender, RoutedEventArgs e)
        {
            if (insMode && stackPtr!=null)
            {
                if (stackPtr.Next != null)
                {
                    stackPtr = stackPtr.Next;
                    inputDisplayBox.Text = stackPtr.Value;
                }
                else
                {
                    inputDisplayBox.Text = "";
                }
            }
        }

    }
}